<?php
/**
 * CompanySetting Model Class File
 *
 * @package
 * @author
 */

/**
 * This is the model class for table "CompanySetting".
 *
 * The followings are the available columns in table 'CompanySetting':
 * @property integer $id
 * @property integer $userId
 * @property string $companyName
 * @property string $companyLegalForm
 * @property string $companyGoal
 * @property string $addressLine1
 * @property string $addressLine2
 * @property string $postCode
 * @property string $town
 * @property string $country
 * @property string $bankAccountOwner
 * @property string $bankName
 * @property string $bankNbr
 * @property string $bankAccountNbr
 *
 * @package
 * @author
 */
class CompanySetting extends MyModel
{
    // {{{ *** Members ***

    /**
     * List of fields composing the record name (separated by ;)
     * @var string
     */
    var $nameFields = "id";

    /**
     * Name format string (e.g. '{1} (#{2})' 
     * @var string
     */
    var $nameFormat = "{1}";
    
    /**
     * Does this model use record revisions?
     * @var bool
     */
    var $usesRevision   = false;

    /**
     * Revision number of current record
     * @var int
     */
    var $recordRevision;

    /**
     * Does this model use logging?
     * @var bool
     */
    var $usesLog        = false;

    public $logoFile;

    // }}} 
    // {{{ *** Methods ***
    // {{{ model
    /**
     * Returns the static model of the specified AR class.
     * @return UserSetting the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    } // }}} 
    // {{{ tableName
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{CompanySetting}}';
    } // }}} 
    // {{{ rules
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('userId', 'required','on'=>'insert, update'),
            array('userId', 'numerical', 'integerOnly'=>true),
            array('companyName, companyLegalForm, addressLine1, town, country, bankAccountOwner, bankName, companyPhoneNbr, companyFaxNbr, hourRate1Description, hourRate2Description', 'length', 'max'=>255),
            array('postCode, taxNbr, vatNbr, bankNbr, bankAccountNbr', 'length', 'max'=>20),
            array('addressLine2, bankAccountTransactionProfile, accountFrame', 'safe'),
            array('companyName, companyLegalForm, companyGoal, addressLine1, postCode, town, country, companyPhoneNbr, currency,termsOfPayment, hourRate1Description, hourRate1Amount','required','on'=>'changeCompanyData'),
            array('currency','length','max'=>3,'on'=>'changeCompanyData'),
            array('laborValuesPerHour, lastRepairOrderNbrThisYear, lastInvoiceNbrThisYear, lastQuoteNbrThisYear','numerical','on'=>'changeCompanyData'),
            array('repairOrderNbrTemplate, lastRepairOrderNbrThisYear, invoiceNbrTemplate, lastInvoiceNbrThisYear, quoteNbrTemplate, lastQuoteNbrThisYear, hourRate2Description, hourRate2Amount, laborValuesPerHour', 'safe','on'=>'changeCompanyData'),
            array('vehicleField1Label, vehicleField2Label, vehicleField3Label, vehicleField4Label, vehicleField5Label','length','max'=>50,'on'=>'changeCompanyData'), 
            array('reminderLevel1Note, reminderLevel2Note, reminderLevel3Note','safe','on'=>'changeCompanyData'),
            array('reminderLevel1Fee, reminderLevel2Fee, reminderLevel3Fee','numerical','on'=>'changeCompanyData'),
            array(' bankAccountOwner, bankName, bankNbr, bankAccountNbr', 'required', 'on'=>'changeBankAccount'),
            array('isVirgin','safe','on'=>'insert, changeCompanyData'),
            array('logoFile', 'safe', 'on'=>'changeCompanyLogo'),
            array('logoFile', 'file', 'types'=>'jpg, gif, png', 'on'=>'changeCompanyLogo'),
            array('invoicePDFSettings','safe','on'=>'changeInvoicePDF'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, userId, companyName, companyLegalForm, companyGoal, addressLine1, addressLine2, postCode, town, country, bankAccountOwner, bankName, bankNbr, bankAccountNbr', 'safe', 'on'=>'search'),
        );
    } // }}} 
    // {{{ beforeSave
    /**
     * Saves the name, size, type and data of the uploaded file
     */
    public function beforeSave()
    {
        if($file=CUploadedFile::getInstance($this,'logoFile'))
        {
            $this->logoFileName     = $file->name;
            $this->logoFileType     = $file->type;
            $this->logoFileSize     = $file->size;
            $this->logoFileContent  = file_get_contents($file->tempName);
        } 
        return parent::beforeSave(); 
    } // }}} 
    // {{{ relations
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'masterUser'=>array(self::BELONGS_TO,'User','userId'),
        );
    } // }}} 
    // {{{ attributeLabels
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => Yii::t('site','ID'),
            'userId' => Yii::t('site','User'),
            'companyName' => Yii::t('site','Company Name'),
            'companyLegalForm' => Yii::t('site','Company Legal Form'),
            'companyGoal' => Yii::t('site','Company Goal'),
            'addressLine1' => Yii::t('site','Address Line1'),
            'addressLine2' => Yii::t('site','Address Line2'),
            'postCode' => Yii::t('site','Post Code'),
            'town' => Yii::t('site','Town'),
            'country' => Yii::t('site','Country'),
            'companyPhoneNbr' => Yii::t('site','Phone No.'),
            'companyFaxNbr' => Yii::t('site','Fax No.'),
            'logoFile' => Yii::t('site','Company Logo File'),
            'taxNbr' => Yii::t('site','Tax No.'),
            'vatNbr' => Yii::t('site','VAT No.'),
            'bankAccountOwner' => Yii::t('site','Bank Account Owner'),
            'bankName' => Yii::t('site','Bank Name'),
            'bankNbr' => Yii::t('site','Bank No.'),
            'bankAccountNbr' => Yii::t('site','Bank Account No.'),
            'repairOrderNbrTemplate' => Yii::t('site','Template for New Repair Order No.'),
            'lastRepairOrderNbrThisYear' => Yii::t('site','Last Repair Order No. used this year'),
            'invoiceNbrTemplate' => Yii::t('site','Template for New Invoice No.'),
            'lastInvoiceNbrThisYear' => Yii::t('site','Last Invoice No. used this year'),
            'quoteNbrTemplate' => Yii::t('site','Template for New Quote No.'),
            'lastQuoteNbrThisYear' => Yii::t('site','Last Quote No. used this year'),
            'currency' => Yii::t('site','Currency'),
            'termsOfPayment' => Yii::t('site','Terms of Payment'),
            'hourRate1Description' => Yii::t('site','Hour Rate 1 Description'),
            'hourRate1Amount' => Yii::t('site','Hour Rate 1 Amount'),
            'hourRate2Description' => Yii::t('site','Hour Rate 2 Description'),
            'hourRate2Amount' => Yii::t('site','Hour Rate 2 Amount'),
            'laborValuesPerHour' => Yii::t('site','Labor Values per Hour'),
            'vehicleField1Label' => Yii::t('site','Vehicle Field 1 Label'),
            'vehicleField2Label' => Yii::t('site','Vehicle Field 2 Label'),
            'vehicleField3Label' => Yii::t('site','Vehicle Field 3 Label'),
            'vehicleField4Label' => Yii::t('site','Vehicle Field 4 Label'),
            'vehicleField5Label' => Yii::t('site','Vehicle Field 5 Label'),
            'reminderLevel1Fee' => Yii::t('site','Reminder Level 1 Fee'),
            'reminderLevel1Note' => Yii::t('site','Reminder Level 1 Note'),
            'reminderLevel2Fee' => Yii::t('site','Reminder Level 2 Fee'),
            'reminderLevel2Note' => Yii::t('site','Reminder Level 2 Note'),
            'reminderLevel3Fee' => Yii::t('site','Reminder Level 3 Fee'),
            'reminderLevel3Note' => Yii::t('site','Reminder Level 3 Note'),
        );
    } // }}} 
    // {{{ search
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('userId',$this->userId);
        $criteria->compare('companyName',$this->companyName,true);
        $criteria->compare('companyLegalForm',$this->companyLegalForm,true);
        $criteria->compare('companyGoal',$this->companyGoal,true);
        $criteria->compare('taxNbr',$this->taxNbr,true);
        $criteria->compare('addressLine1',$this->addressLine1,true);
        $criteria->compare('addressLine2',$this->addressLine2,true);
        $criteria->compare('postCode',$this->postCode,true);
        $criteria->compare('town',$this->town,true);
        $criteria->compare('country',$this->country,true);
        $criteria->compare('bankAccountOwner',$this->bankAccountOwner,true);
        $criteria->compare('bankName',$this->bankName,true);
        $criteria->compare('bankNbr',$this->bankNbr,true);
        $criteria->compare('bankAccountNbr',$this->bankAccountNbr,true);
        return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria));
    } // }}} 
    // {{{ getAddress
    public function getAddress()
    {
        $result = '';
        if($this->addressLine1!='')
            $result .= $this->addressLine1;
        if($this->addressLine2!='')
            $result .= "\n".$this->addressLine2;
        if($this->postCode!='')
            $result .= "\n".$this->postCode;
        if($this->town!='')
            $result .= " ".$this->town;
        if($this->country!='')
            $result .= "\n".$this->country;
        return $result;
    } // }}} 
    // {{{ getMechanicsOptions
    public function getMechanicsOptions()
    {
        $criteria = new CDbCriteria;
        $criteria->order = 'lastName, firstName';
        $criteria->addCondition('companyId='.$this->id);
        $models = User::model()->findAll($criteria);
        $result = array(''=>Yii::t('site','(All)'));
        foreach($models as $model) {
            if(Yii::app()->authManager->checkAccess('mechanic',$model->id))
                $result[$model->id] = $model->lastFirstName;
        }
        return $result;
    } // }}} 
    // {{{ getWorkplacesOptions
    public function getWorkplacesOptions()
    {
        $criteria = new CDbCriteria;
        $criteria->order = 'name';
        $criteria->addCondition('companyId='.$this->id);
        $models = WorkPlace::model()->findAll($criteria);
        $result = array(''=>Yii::t('site','(All)'));
        foreach($models as $model) {
            $result[$model->id] = $model->recordName;
        }
        return $result;
    } // }}} 
    // }}} 
}

/* vim: set ai sw=4 sts=4 et fdm=marker fdc=4 */ 
?>
